(function($, doc) {
	$.init();
	//var wallet = ethers.Wallet.createRandom();
	//console.log(JSON.stringify(wallet));
	//doc.getElementById('word').value = wallet.mnemonic;
	$.plusReady(function() {
		var user = app.getState();
		var uid = user.uid;
		var ethwordbox = doc.getElementById('ethword');
		var btcwordbox = doc.getElementById('btcword');
		var eoswordbox = doc.getElementById('eosword');
		var namebox = doc.getElementById('name');
		var passwdbox = doc.getElementById('passwd');
		var repasswdbox = doc.getElementById('repasswd');
		var ethwallet = ethers.Wallet.createRandom();
		//var provider = new providers.JsonRpcProvider('http://localhost:8545', network);
		var btcword = bitscan.getWord();
		var eosword = bitscan.getWord();
		var ethword = ethwallet.mnemonic;
		//console.log(JSON.stringify(ethwallet));
		ethwordbox.value = ethword;
		btcwordbox.value = btcword;
		eoswordbox.value = eosword;
		//console.log(eosword);
		//console.log(btcword);
		doc.getElementById('sub').addEventListener('tap', function() {
//			var my = 1;
//			[].forEach.call(doc.getElementsByClassName("mui-active"), function(item, index) {
//				if(index) {
//					my = item.id.replace("item", "") * 1;
//				}
//			});

			// modify by yaozy  修复密码框启用明文显示后提示助记词不能为空的问题
			var my;
			
			my = doc.querySelector('.mui-input-row.mui-active');
			my = (my && (my = my.id) && my.replace('item', '') | 0) || 1;

			if(my == 1) {
				var word = ethwordbox.value;
			} else if(my == 2) {
				var word = btcwordbox.value;
			} else if(my == 3) {
				var word = eoswordbox.value;
			}
			var name = namebox.value;
			var passwd = passwdbox.value;
			if(!name) {
				$.toast("钱包名称不能为空");
				return;
			}
			if(!word) {
				$.toast("助记词不能为空");
				return;
			}
			if(app.checkPwd(passwd, 6) === false) {
				$.toast('钱包密码必须字母加数字6位以上');
				return;
			}
			if(passwd !== repasswdbox.value) {
				$.toast('两次输入密码不一致！');
				return;
			}
			if(my == 1) {

				var adddata = {
					name: name + '(eth)',
					address: ethwallet.address,
					passwd: md5(passwd),
					privkey: ethwallet.privateKey,
					mnemonic: ethwallet.mnemonic,
					path: ethwallet.path
				};
				ethscan.addWallet(adddata, 'eth');

				$.toast("钱包创建成功!");
				plus.webview.currentWebview().close();

			} else if(my == 2) {
				plus.nativeUI.showWaiting("钱包创建中...");
				bitscan.createWallet(word, md5(passwd), function(rt) {
					plus.nativeUI.closeWaiting();
					var adddata = {
						'name': name + '(btc)',
						'privkey': rt.sec,
						'address': rt.address,
						'mnemonic': word,
						'passwd': md5(passwd)
					};
					ethscan.addWallet(adddata, 'btc');
					$.toast("钱包创建成功!");
					plus.webview.currentWebview().close();

				});
			} else if(my == 3) {
				//plus.nativeUI.showWaiting("钱包创建中...");

				var ecc = eosjs_ecc;
				var privkey = ecc.seedPrivate(word);
				var addr = ecc.privateToPublic(privkey);
				//console.log(privkey);
				//console.log(addr);
				var adddata = {
					'name': name + '(eos)',
					'privkey': privkey,
					'address': addr,
					'mnemonic': word,
					'passwd': md5(passwd)
				};
				ethscan.addWallet(adddata, 'eos');
				$.toast("钱包创建成功!");
				plus.webview.currentWebview().close();
			}

		});
	});

}(mui, document));